//
//  PickerViewController.m
//  PickerViewController
//
//  Created by ET|冰琳 on 2017/2/14.
//  Copyright © 2017年 IB. All rights reserved.
//

#import "PickerViewController.h"

@interface PickerViewController ()

@property (nonatomic, strong) NSMutableArray *private_selectedRows;

@end


@implementation PickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, screenWidth, self.containerView.frame.size.height - 44)];
    
    [self.containerView addSubview:self.pickerView];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    
    for (int i = 0; i < self.private_selectedRows.count; i ++) {
        
        NSNumber *row = self.private_selectedRows[i];
        [self.pickerView selectRow:[row integerValue] inComponent:i animated:NO];
    }
}


- (NSMutableArray *)private_selectedRows {
    if (_private_selectedRows == nil) {
        _private_selectedRows = [NSMutableArray new];
    }
    return _private_selectedRows;
}

- (void)setPickerDatas:(NSArray<NSArray *> *)pickerDatas {
    
    _pickerDatas = pickerDatas;
    
    if (self.selectInfo.count == 0) {
        return;
    }
    
    for (int i = 0; i < pickerDatas.count; i ++) {
       
        NSArray *datas = [pickerDatas objectAtIndex:i];
        
        NSString * textSelect = self.textCallback(self.selectInfo[i], -1, -1);
        
        for (int j = 0; j < datas.count; j ++) {
            
            id obj = datas[j];
            NSString *objText = self.textCallback(obj, -1, -1);
            if ([objText isEqualToString:textSelect]) {
                [self.private_selectedRows addObject:@(j)];
                continue;
            }
        }
    }
}

- (void)confirmActioned {
    
    NSMutableArray *result = [NSMutableArray new];
    NSMutableArray *resultIndexes = [NSMutableArray new];
    
    
    for (int component = 0; component < self.pickerView.numberOfComponents; component ++) {
        
        NSInteger selectedRow = [self.pickerView selectedRowInComponent:component];
        [resultIndexes addObject:@(selectedRow)];
        
        if (self.pickerDatas.count) {
            NSArray *componentData = self.pickerDatas[component];
            id select = [componentData objectAtIndex:selectedRow];
            [result addObject:select];
        }
    }
    
    if (self.resultBlock) {
        if (result.count == 0) {
            result = nil;
        }
        self.resultBlock(result, resultIndexes);
        [self dismissViewControllerAnimated:true completion:nil];
    }

}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (self.pickerDatas != nil) {
        return self.pickerDatas.count;
    }
    if (self.componentCallback != nil) {
        return self.componentCallback();
    }
    return 0;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (self.pickerDatas != nil) {
        NSArray *sectionData = self.pickerDatas[component];
        return sectionData.count;
    }
    
    if (self.rowsCallback != nil) {
        if (component == 0) {
        
            return self.rowsCallback(component, -1);
        }else {
            return self.rowsCallback(component, [pickerView selectedRowInComponent:component - 1]);
        }
        
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [self private_titleForComponent:component row:row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    
    NSString *title = [self private_titleForComponent:component row:row];
    
    if (view != nil) {
        UILabel *label = (UILabel*)view;
        return label;
    }
    else{
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0.5, [UIScreen mainScreen].bounds.size.width, 43)];
        label.font = [UIFont systemFontOfSize:15.0];
        //label.textColor = [UIColor colorWithRed:0x80/255.0 green:0x6e/255.0 blue:0 alpha:1.0];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = title;
        return label;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 44.0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    for (NSInteger i = component + 1; i < [pickerView numberOfComponents]; i ++) {
        [pickerView reloadComponent:i];
    }
}

- (NSString *)private_titleForComponent:(NSInteger)component row: (NSInteger)row {
    
    if (self.pickerDatas != nil) {
        NSArray *componentData = self.pickerDatas[component];
        return self.textCallback(componentData[row], component, row);
    }

    return self.textCallback(nil, component, row);
}

@end
